package cn.dy.workatten.dao;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.dy.base.framework.db.DBConnectPool;
import cn.dy.base.framework.db.DBUtil;
import cn.dy.base.framework.db.util.DBExecuteUtil;
import cn.dy.workatten.pojo.AuditingFlow;

public class AuditingFlowDao {

	protected static Logger logger = LoggerFactory.getLogger(AuditingFlowDao.class);

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	private NamedParameterJdbcTemplate namedJdbcTemplate;

	public AuditingFlowDao() {
		this.dataSource = DBConnectPool.create().getDataSource();
		if (this.jdbcTemplate == null)
			this.jdbcTemplate = new JdbcTemplate(this.dataSource);
		if (this.namedJdbcTemplate == null) {
			this.namedJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource);
		}
	}
	
	
	/**
	 * 获得自增系列
	 * @return
	 */
	public long getUniqueId() {
		long result = 0L;
		try {
			result = DBUtil.getSequence(new JdbcTemplate(this.dataSource), "seq_bz_auditing_flow");
		} catch (DataAccessException e) {
			logger.error("获得审核流程自动生成的ID值失败", e);
		}
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param scheduleCfg
	 * @return
	 */
	public int addAuditingFlow(AuditingFlow auditingFlow) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;

		try {
			String sql = "insert into bz_auditing_flow(id,flow_type,flow_sts,application_id,create_id,create_time,cur_detail_id) values(:id,:flow_type,:flow_sts,:application_id,:create_id,:create_time,:cur_detail_id)";

			Map resultMap = new HashMap();
//			resultMap.put("id", getUniqueId());
			resultMap.put("id", auditingFlow.getId());
			resultMap.put("flow_type", auditingFlow.getFlow_type());
			resultMap.put("flow_sts", auditingFlow.getFlow_sts());
			resultMap.put("application_id", auditingFlow.getApplication_id());
			resultMap.put("create_id", auditingFlow.getCreate_id());
			resultMap.put("create_time", auditingFlow.getCreate_time());
			resultMap.put("cur_detail_id", auditingFlow.getCur_detail_id());
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("添加审核流程失败", e);
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public int updateAuditingFlow(AuditingFlow auditingFlow) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;
		try {
			String sql = "update bz_auditing_flow set flow_type=:flow_type,flow_sts=:flow_sts,application_id=:application_id,create_id=:create_id,create_time=:create_time,cur_detail_id=:cur_detail_id where id=:id";

			Map resultMap = new HashMap();
			resultMap.put("id", Long.valueOf(auditingFlow.getId()));
			resultMap.put("flow_type", auditingFlow.getFlow_type());
			resultMap.put("flow_sts", auditingFlow.getFlow_sts());
			resultMap.put("application_id", auditingFlow.getApplication_id());
			resultMap.put("create_id", auditingFlow.getCreate_id());
			resultMap.put("create_time", auditingFlow.getCreate_time());
			resultMap.put("cur_detail_id", auditingFlow.getCur_detail_id());
		} catch (DataAccessException e) {
			logger.error("修改审核流程失败", e);
		}
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public int deleteAuditingFlowById(long id) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;
		try {
			String sql = " delete from bz_auditing_flow where id=:id";
			Map resultMap = new HashMap();
			resultMap.put("id", Long.valueOf(id));
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("删除审核流程失败", e);
		}
		return result;
	}
	

	/**
	 * 根据id查询审核流程信息
	 * 
	 * @param id
	 * @return
	 */
	public AuditingFlow getAuditingFlowById(long id) {
		DBExecuteUtil db = new DBExecuteUtil();
		AuditingFlow auditingFlow = new AuditingFlow();
		try {
			String sql = "select * from bz_auditing_flow where id=:id ";
			
			Map parameters = new HashMap();
			parameters.put("id", Long.valueOf(id));
			auditingFlow = (AuditingFlow) db.queryObject(sql, parameters, AuditingFlow.class);
		} catch (DataAccessException e) {
			logger.error("根据id获得审核流程失败", e);
		}
		return auditingFlow;
	}
	
	
	/**
	 * 根据application_id查询审核流程信息
	 * 
	 * @param id
	 * @return
	 */
	public AuditingFlow getAuditingFlowByApId(long attend_application_id) {
		DBExecuteUtil db = new DBExecuteUtil();
		AuditingFlow auditingFlow = new AuditingFlow();
		try {
			String sql = "select * from bz_auditing_flow where application_id=:application_id ";
			
			Map parameters = new HashMap();
			parameters.put("application_id", Long.valueOf(attend_application_id));
			auditingFlow = (AuditingFlow) db.queryObject(sql, parameters, AuditingFlow.class);
		} catch (DataAccessException e) {
			logger.error("根据attend_application_id获得审核流程失败", e);
		}
		return auditingFlow;
	}
	
}
